package com.hellogood.yaohuo.server.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hellogood.yaohuo.server.db.DBHelper;
import com.hellogood.yaohuo.shared.entity.Group;
import com.hellogood.yaohuo.shared.entity.GroupUser;
import com.hellogood.yaohuo.shared.entity.User;
import com.hellogood.yaohuo.shared.exception.DatabaseException;

public class GroupUserDAO extends DBHelper {

	@Override
	public List<?> initializeObject(ResultSet rs) throws SQLException {
		List<GroupUser> list = new ArrayList<GroupUser>();
		if (rs != null) {
			while (rs.next()) {
				GroupUser groupUser = new GroupUser();
				groupUser.setStaffCode(rs.getString("StaffCode"));
				groupUser.setStaffName(rs.getString("StaffName"));
				groupUser.setGroupCode(rs.getString("GroupCode"));
				list.add(groupUser);
			}
		}
		return list;
	}

	public List<GroupUser> getUserGroup() {
		String sql = "SELECT * FROM GroupUser";
		List<?> list = super.query(sql);
		return (List<GroupUser>) list;
	}

	public List<GroupUser> getUserByGroup(String groupCode) {
		String sql = "SELECT * FROM GroupUser WHERE GroupCode = '" + groupCode + "'";
		List<?> list = super.query(sql);
		return (List<GroupUser>) list;
	}

	public String addUserToGroup(User u, Group g) {
		String sql = "INSERT INTO GroupUser(GroupCode,StaffCode,StaffName) VALUES('" + g.getGroupCode() + "','" + u.getStaffCode() + "','"
				+ u.getStaffName() + "')";
		try {
			super.execute(sql);
		} catch (DatabaseException e) {
			e.printStackTrace();
			return e.getMessage();
		}
		return "";
	}

	public String removeUserFromGroup(String groupCode) {
		String sql = "DELETE FROM GroupUser WHERE GroupCode = '" + groupCode + "'";
		try {
			super.execute(sql);
		} catch (DatabaseException e) {
			e.printStackTrace();
			return e.getMessage();
		}
		return "";
	}

	public String addUserToGroup(List<User> users, String groupCode) {
		List<String> sqls = new ArrayList<String>();
		if (users != null && !users.isEmpty()) {
			String sql = "DELETE FROM GroupUser WHERE GroupCode = '" + groupCode + "'";
			sqls.add(sql);
			for (User u : users) {
				sql = "INSERT INTO GroupUser(GroupCode,StaffCode,StaffName) VALUES('" + groupCode + "','" + u.getStaffCode() + "','"
						+ u.getStaffName() + "')";
				sqls.add(sql);
			}
			try {
				super.executeBatch(sqls);
			} catch (DatabaseException e) {
				e.printStackTrace();
				return e.getMessage();
			}
		}
		return "";
	}

	public String removeUserFromGroup(List<User> users, String groupCode) {
		List<String> sqls = new ArrayList<String>();
		if (users != null && !users.isEmpty()) {
			for (User u : users) {
				String sql = "DELETE FROM GroupUser WHERE GroupCode = '" + groupCode + "' AND StaffCode = '" + u.getStaffCode() + "'";
				sqls.add(sql);
			}
			try {
				super.executeBatch(sqls);
			} catch (DatabaseException e) {
				e.printStackTrace();
				return e.getMessage();
			}
		}
		return "";
	}
}
